﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using MarkdownDeep;

namespace DNT.RavenQA.Web.Infrastructure.Extensions
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString ClientIdFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                                                   Expression<Func<TModel, TProperty>> expression)
        {
            return MvcHtmlString.Create(htmlHelper.ViewContext
                                                  .ViewData
                                                  .TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression)));
        }

        private static readonly Markdown _markdown = new Markdown();

        public static IHtmlString Markdown(this HtmlHelper helper, String text)
        {
            return new MvcHtmlString(_markdown.Transform(text));
        }

        public static Boolean MatchRouteData(this HtmlHelper helper, String controllerName, params String[] actionNames)
        {
            var currentControllerName = (String)helper.ViewContext.RouteData.Values["controller"];
            var currentActionName = (String)helper.ViewContext.RouteData.Values["action"];

            if (!String.Equals(currentControllerName, controllerName, StringComparison.OrdinalIgnoreCase))
                return false;

            return actionNames.Any(actionName => String.Equals(actionName, 
                                                               currentActionName, 
                                                               StringComparison.OrdinalIgnoreCase));
        }
    }
}